using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Urbania.Utilities;

namespace Urbania.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StartMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private GUI.LuaVM lua;
        private GUI.GUIContainer container;
        private SpriteBatch spriteBatch;
        private Animation titleAnim;

        public StartMenu(Game game, ref GUI.LuaVM lua)
            : base(game)
        {
            this.lua = lua;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            container = new GUI.GUIContainer();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Loader.LoadTex("Tiles\\Title");
            Loader.LoadFont("Font\\default");
            Loader.LoadFont("Font\\30point");
            Loader.LoadScriptFile("StartGame");
            Loader.LoadScriptFile("Scripts\\Quit.lua");
            titleAnim = new Animation();
            titleAnim.SpriteSheet = Utilities.Loader.GetTex("Title");
            titleAnim.FrameRate = 2;
            titleAnim.RepeatFrameIndex = 0;
            titleAnim.FrameHeight = 109;
            titleAnim.FrameWidth = 96;
            titleAnim.FrameIndex = 0;
            titleAnim.IsRunning = true;

            container.AddFrame(GUI.XML.XMLHandler.DeSerialize<GUI.Elements.Frame>("XML\\StartMenu.xml", ref lua));

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            container.Update(gameTime);
            Utilities.MouseWrap.Update();
            //titleAnim.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            container.Draw(gameTime, spriteBatch);
            //spriteBatch.Draw(titleAnim, new Vector2(0, 0), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
